﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Globalization;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.APP.Classes;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PerformanceAgreementInfo : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private Main main = new Main();
        private SsMng creatby = new SsMng();
        private IPerformanceAgreementBLL CreatPerformanceAgreement = new PerformanceAgreementBLL();

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);
            IncludeJava();

            if (!IsUserControlPostBack)
            {
                this.ViewState.Add("IsUserControlPostBack", true);

                if (Request.QueryString["sta"] != null)
                    this.lblSta.Text = Request.QueryString["sta"].ToString();

                lblEmpSeq.Text = Session["emp_seq"].ToString();
                LoadPerformanceAgreement();
            }
        }

        public void IncludeJava()
        {
            HtmlGenericControl script = new HtmlGenericControl();
            script.TagName = "script";
            script.Attributes.Add("type", "text/javascript");
            script.Attributes.Add("src", "../../Scripts/jsfunction.js");
            Page.Header.Controls.Add(script);
        }

        private void LoadPerformanceAgreement()
        {
            lblEmpSeq.Text = Session["emp_seq"].ToString();
            string strQueryString = lblEmpSeq.Text;

            RadGrid1.DataSource = CreatPerformanceAgreement.Search(strQueryString, null, null, null, null, null);
            RadGrid1.DataBind();
        }

        protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridCommandItem)
            {
                GridCommandItem commandItem = e.Item as GridCommandItem;
                LinkButton button = commandItem.FindControl("cmdAddNew") as LinkButton;

                if (this.lblSta.Text == "v")
                {
                    button.Enabled = false;
                    button.Visible = true;
                    button.Text = "";
                }
            }
        }

        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            lblEmpSeq.Text = Session["emp_seq"].ToString();
            string strQueryString = lblEmpSeq.Text;

            RadGrid1.DataSource = CreatPerformanceAgreement.Search(strQueryString, null, null, null, null, null);
        }

        protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int intAgreementSeq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["perf_agreement_seq"]);
                string strQueryString = main.WebString((e.Item as GridDataItem)["emp_seq"].Text);

                bool CheckValueDelete = CreatPerformanceAgreement.CheckValueDelete(intAgreementSeq);
                if (CheckValueDelete == true)
                {
                    CreatPerformanceAgreement.Delete(creatby.EmpSeqI, intAgreementSeq, strQueryString);
                    main.MessageAlert(Page, "ลบข้อมูลรียบร้อยแล้ว");
                }
            }
            LoadPerformanceAgreement();
        }
    }
}
